﻿//----------------------------------------------------------------------------------------------------
// Debug console
//----------------------------------------------------------------------------------------------------
package HG.Visual
{
	import flash.display.*;
	import flash.events.*;
	
	import HG.Debug;
	import HG.GameService;
	import HG.Managers.BindingManager;
	
	import HG.Visual.DebugConsoleLine;
	
	public class DebugConsole extends MovieClip
	{
		var m_outputLines:Array;
		var m_bindingManager:BindingManager;
		
		//----------------------------------------------------------------------------------------------------
		function DebugConsole()
		{
			super();
			
			m_outputLines = new Array();
			
			loaderInfo.addEventListener(Event.INIT, onInit);
			
			Debug.RegisterConsole(this);
			
			m_bindingManager = GameService.GetInstance().GetBindingManager();
			
			// hide by default.
			ToggleDisplay();
		}
		
		//----------------------------------------------------------------------------------------------------
		public function onInit(e:Event)
		{
			Input.TXT_Input.addEventListener(KeyboardEvent.KEY_UP, OnKey);
			stage.addEventListener(KeyboardEvent.KEY_UP, GlobalKeyUp);
		}
		
		//----------------------------------------------------------------------------------------------------
		private function OnKey(event:KeyboardEvent)
		{
			if(event.charCode == 13)
			{
				ProcessInput();
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		private function GlobalKeyUp(event:KeyboardEvent)
		{
			if(event.charCode == 96)
			{
				ToggleDisplay();
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		private function ToggleDisplay()
		{
			Input.visible = !Input.visible;
			
			if (Input.visible)
			{
				stage.focus = Input.TXT_Input;
				Input.TXT_Input.setSelection(0, 0);
				Input.TXT_Input.text = "";
			}
			
			Content.y = 0;
			if (Input.visible)
			{
				Content.y = Input.height;
			}
			
			var index:Number = 0;
			var listLength:Number = m_outputLines.length;
			for(index = 0; index < listLength; ++index)
			{
				m_outputLines[index].SetVisible(Input.visible);
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		private function ProcessInput()
		{
			var input:String = Input.TXT_Input.text;
			m_bindingManager.HandleInput(input);
			Input.TXT_Input.text = "";
		}
		
		//----------------------------------------------------------------------------------------------------
		public function Output(message:String)
		{
			var output:DebugConsoleLine = new DebugConsoleLine(message);
			
			m_outputLines.unshift(output);
			output.y = 0;
			output.x = 0;
			
			var index:Number = 0;
			var listLength:Number = m_outputLines.length;
			for(index = 0; index < listLength; ++index)
			{
				var lastElementBottom:Number = 0;
				
				if (index > 0)
					lastElementBottom = m_outputLines[index - 1].y + (m_outputLines[index - 1].height - 2);
					
				m_outputLines[index].SetY(lastElementBottom);
				
				if (Input.visible)
					m_outputLines[index].SetVisible(true);
			}
			
			this.Content.Content.addChild(output);
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}